package cn.coder.jdbc2.session;

import java.sql.SQLException;
import java.util.List;

import cn.coder.jdbc2.config.Configuration;
import cn.coder.jdbc2.config.MappedStatement;
import cn.coder.jdbc2.executor.Executor;
import cn.coder.jdbc2.executor.SimpleExecutor;

public class DefaultSqlSession implements SqlSession {

	private Configuration configuration;
    private Executor executor;
    
    public DefaultSqlSession(Configuration configuration, String executorType) {
        this.configuration = configuration;
        
        if(executorType == null) {
            this.executor = new SimpleExecutor();            
        }else {
            ////
        }
    }

    @Override
    public <T> T selectOne(String statementId, Object params) throws SQLException {
        // TODO Auto-generated method stub
        List<T> list = this.selectList(statementId, params);
        
        if (list.size() == 1) {
            return list.get(0);
        } else if (list.size() > 1) {
            throw new RuntimeException(
                    "通过selectOne()方法，期待返回一个结果, 但实际返回结果数为: " + list.size());
        } else {
            return null;
        }
    }

    @Override
    public <T> List<T> selectList(String statementId, Object params) throws SQLException {
        // TODO Auto-generated method stub
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return executor.selectList(configuration, mappedStatement, params);
    }

}
